/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.chart;

import net.cachapa.libra.util.MyDate;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;

public class DateImageGenerator {
	public static final int TYPE_WEEK_YEAR = 0;
	public static final int TYPE_MONTH_YEAR = 1;
	public static final int TYPE_YEAR = 2;
	
	private static final int YEAR_TEXT_COLOUR = 0xffaaaaaa;
	private static final int MONTH_TEXT_COLOUR = 0xffffffff;
	private static final int DAY_TEXT_COLOUR = 0xffffffff;
	private static final int DAY_TEXT_SIZE = 18;
	private static final int MONTH_TEXT_SIZE = 18;
	private static final int YEAR_TEXT_SIZE = 30;
	
	private static Paint paintDay = null, paintMonth, paintYear;
	
	private static void createPainters() {
		paintDay = new Paint();
		paintDay.setAntiAlias(true);
		paintDay.setColor(DAY_TEXT_COLOUR);
		paintDay.setTextAlign(Align.CENTER);
		paintDay.setTextSize(DAY_TEXT_SIZE);
		
		paintMonth = new Paint();
		paintMonth.setAntiAlias(true);
		paintMonth.setColor(MONTH_TEXT_COLOUR);
		paintMonth.setTextAlign(Align.CENTER);
		paintMonth.setTextSize(MONTH_TEXT_SIZE);

		paintYear = new Paint();
		paintYear.setAntiAlias(true);
		paintYear.setColor(YEAR_TEXT_COLOUR);
		paintYear.setTextAlign(Align.CENTER);
		paintYear.setTextSize(YEAR_TEXT_SIZE);
		paintYear.setTypeface(Typeface.DEFAULT_BOLD);
	}

	public static Bitmap GenerateImage(MyDate date, int imageType, float scale) {
		if (paintDay == null) {
			createPainters();
		}
		
		Bitmap bitmap = null;
		switch (imageType) {
		case TYPE_WEEK_YEAR:
			// The label for the weekly chart is the same as for the monthly chart
			bitmap = generateMonthYear(date, scale);
			break;
		case TYPE_MONTH_YEAR:
			bitmap = generateMonthYear(date, scale);
			break;
		case TYPE_YEAR:
			bitmap = generateYear(date, scale);
			break;
		}
		return bitmap;
	}
	
	public static Bitmap GenerateImage(MyDate startDate, MyDate endDate, float scale) {
		if (paintDay == null) {
			createPainters();
		}
		paintDay.setTextSize(DAY_TEXT_SIZE * scale);
		
		String label = startDate.toMediumString() + " - " + endDate.toMediumString();
		
		int width = (int) paintDay.measureText(label);
		int x = (int) (width / 2);
		int yLabel = (int) (-paintDay.ascent() + paintDay.descent());
		
		Bitmap bitmap = Bitmap.createBitmap(width, yLabel, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawText(label, x, -paintDay.ascent(), paintDay);
		
		return bitmap;
	}

	private static Bitmap generateMonthYear(MyDate date, float scale) {
		if (paintDay == null) {
			createPainters();
		}
		paintMonth.setTextSize(MONTH_TEXT_SIZE * scale);
		paintYear.setTextSize(YEAR_TEXT_SIZE * scale);
		
		String month = date.getMonthName();
		String year = String.valueOf(date.getYear());
		
		int width = (int) Math.max(paintMonth.measureText(month), paintYear.measureText(year));
		int x = (int) (width / 2);
		int yMonth = (int) (-paintMonth.ascent() + paintMonth.descent());
		int yYear = (int) (yMonth - paintMonth.ascent() + 10 * scale);
		
		Bitmap bitmap = Bitmap.createBitmap(width, yYear, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawText(month, x, yMonth, paintMonth);
		canvas.drawText(year, x, yYear, paintYear);
		
		return bitmap;
	}
	
	private static Bitmap generateYear(MyDate date, float scale) {
		if (paintDay == null) {
			createPainters();
		}
		paintYear.setTextSize(YEAR_TEXT_SIZE * scale);
		
		String year = date.getYear() + "";
		
		int width = (int) paintYear.measureText(year);
		int x = (int) (width / 2);
		int yYear = (int) (-paintYear.ascent() + paintYear.descent());
		
		Bitmap bitmap = Bitmap.createBitmap(width, yYear, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawText(year, x, yYear, paintYear);
		
		return bitmap;
	}
}